﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PPI.Client.Logic;
using PPI.Client.Logic.Inspection;
using PPI.Entities.Inspections;
using PPI.Entities.Remedy;

namespace PPI.Controls.Grid
{
    public partial class RemedyGrid : GridBase
    {
        private DataGridViewTextBoxCell cellRemedyId;
        private DataGridViewTextBoxCell cellInspectionProject;
        private DataGridViewTextBoxCell cellInspectionResult;
        private DataGridViewTextBoxCell cellElectrical;
        private DataGridViewTextBoxCell cellDiscoveredDate;
        private DataGridViewTextBoxCell cellToptype;
        private DataGridViewTextBoxCell cellInspectionType;
        private DataGridViewTextBoxCell cellPreviewInspected;
        private DataGridViewTextBoxCell cellRemedyLastAction;
        private DataGridViewTextBoxCell cellRemedyResolvedDate;
        private DataGridViewTextBoxCell cellRemedyNotes;
        private DataGridViewTextBoxCell cellRemedyAssigned;
        private DataGridViewTextBoxCell cellInspectionMap;
        private DataGridViewTextBoxCell cellInspectionSection;
        private DataGridViewTextBoxCell cellInspectionId;
        private DataGridViewTextBoxCell cellAssetId;
        private List<ERemedyStatus> listRemedyStatus;
        private ERemedyStatus m_remedyStatusNullValue;


        public RemedyGrid()
        {
            InitializeComponent();
            m_ActivateDoubleClick = true;
            m_ColumnId = "RemedyId";
            m_remedyStatusNullValue = new ERemedyStatus(Int32.MinValue, "[Unassigned]");
            SetGridStyle();
        }

        public void SetGridStyle()
        {
            if (listRemedyStatus == null)
            {
                listRemedyStatus = LRemedyStatus.SelectAll();
                listRemedyStatus.Add(m_remedyStatusNullValue);
            }
            

            ShowContextMenu = false;
            cellInspectionProject = new DataGridViewTextBoxCell();
            cellRemedyId = new DataGridViewTextBoxCell();
            cellInspectionResult = new DataGridViewTextBoxCell();
            cellDiscoveredDate = new DataGridViewTextBoxCell();
            cellToptype = new DataGridViewTextBoxCell();
            cellInspectionType = new DataGridViewTextBoxCell();
            cellPreviewInspected = new DataGridViewTextBoxCell();
            cellRemedyLastAction = new DataGridViewTextBoxCell();
            cellRemedyResolvedDate = new DataGridViewTextBoxCell();
            cellRemedyNotes = new DataGridViewTextBoxCell();
            cellRemedyAssigned = new DataGridViewTextBoxCell();
            cellInspectionMap = new DataGridViewTextBoxCell();
            cellInspectionSection = new DataGridViewTextBoxCell();
            cellElectrical = new DataGridViewTextBoxCell();
            cellInspectionId = new DataGridViewTextBoxCell();
            cellAssetId = new DataGridViewTextBoxCell();


            DataGridViewColumn colRemedyId = new DataGridViewColumn(cellRemedyId);
            colRemedyId.Visible = false;
            colRemedyId.DataPropertyName = m_ColumnId;
            colRemedyId.ReadOnly = true;

            DataGridViewColumn colInspectionProject = new DataGridViewColumn(cellInspectionProject);
            colInspectionProject.HeaderText = "Inspection Project";
            colInspectionProject.DataPropertyName = "InspectionProject";
            colInspectionProject.ReadOnly = true;
            colInspectionProject.DefaultCellStyle.NullValue = "-";

            DataGridViewColumn colInspectionId = new DataGridViewColumn(cellInspectionId);
            colInspectionId.HeaderText = "Inspection Visit Number";
            colInspectionId.DataPropertyName = "InspectionId";
            colInspectionId.ReadOnly = true;

            DataGridViewColumn colAssetId = new DataGridViewColumn(cellAssetId);
            colAssetId.HeaderText = "Asset Number";
            colAssetId.DataPropertyName = "AssetId";
            colAssetId.ReadOnly = true;
            colAssetId.DefaultCellStyle.NullValue = "-";

            DataGridViewColumn colInspectionResult = new DataGridViewColumn(cellInspectionResult);
            colInspectionResult.HeaderText = "Inspect Result";
            colInspectionResult.DataPropertyName = "InspectionResult";
            colInspectionResult.ReadOnly = true;
            colInspectionResult.SortMode = DataGridViewColumnSortMode.Automatic;

            DataGridViewColumn colInspectionElectrical = new DataGridViewColumn(cellElectrical);
            colInspectionElectrical.HeaderText = "Electrical Problem";
            colInspectionElectrical.DataPropertyName = "isElectrical";
            colInspectionElectrical.ReadOnly = true;
            colInspectionElectrical.SortMode = DataGridViewColumnSortMode.Automatic;
            colInspectionElectrical.DefaultCellStyle.NullValue = "NO";



            DataGridViewColumn colInspectionMap = new DataGridViewColumn(cellInspectionMap);
            colInspectionMap.HeaderText = "Map";
            colInspectionMap.DataPropertyName = "InspectionMap";
            colInspectionMap.ReadOnly = true;

            DataGridViewColumn colInspectionSection = new DataGridViewColumn(cellInspectionSection);
            colInspectionSection.HeaderText = "Section";
            colInspectionSection.DataPropertyName = "InspectionSection";
            colInspectionSection.ReadOnly = true;
            colInspectionSection.DefaultCellStyle.NullValue = "-";

            DataGridViewColumn colDiscoveredDate = new DataGridViewColumn(cellDiscoveredDate);
            colDiscoveredDate.HeaderText = "Discovered Date";
            colDiscoveredDate.DataPropertyName = "DiscoveredDate";
            colDiscoveredDate.ReadOnly = true;

            DataGridViewColumn colToptype = new DataGridViewColumn(cellToptype);
            colToptype.HeaderText = "Pole Top Type";
            colToptype.DataPropertyName = "PoleToptype";
            colToptype.ReadOnly = true;
            colToptype.DefaultCellStyle.NullValue = "-";

            DataGridViewColumn colInspectionType = new DataGridViewColumn(cellInspectionType);
            colInspectionType.HeaderText = "Inspect Type";
            colInspectionType.DataPropertyName = "InspectionType";
            colInspectionType.ReadOnly = true;

            DataGridViewColumn colPreviusInspected = new DataGridViewColumn(cellPreviewInspected);
            colPreviusInspected.HeaderText = "Previous Inspect";
            colPreviusInspected.DataPropertyName = "PreviusInspect";
            colPreviusInspected.ReadOnly = true;
            colPreviusInspected.Visible = false;

            DataGridViewLinkColumn colInspectedDetails = new DataGridViewLinkColumn();
            colInspectedDetails.HeaderText = "Inspect Details";
            colInspectedDetails.DataPropertyName = "InspectDetails";
            colInspectedDetails.ReadOnly = true;
            colInspectedDetails.UseColumnTextForLinkValue = true;
            colInspectedDetails.Text = "Click";
            colInspectedDetails.LinkColor = Color.DarkBlue;

            DataGridViewComboBoxColumn colRemedyStatus = new DataGridViewComboBoxColumn();
            colRemedyStatus.DataPropertyName = "RemedyStatus";
            colRemedyStatus.ValueMember = "Id";
            colRemedyStatus.DisplayMember = "Name";
            colRemedyStatus.DataSource = listRemedyStatus;
            colRemedyStatus.HeaderText = "Remedy Status";
            colRemedyStatus.DefaultCellStyle.NullValue = m_remedyStatusNullValue;

            DataGridViewColumn colRemedyAssigned = new DataGridViewColumn(cellRemedyAssigned);
            colRemedyAssigned.HeaderText = "Remedy Assigned";
            colRemedyAssigned.DataPropertyName = "RemedyAssigned";
            colRemedyAssigned.DefaultCellStyle.NullValue = "Unassigned";

            DataGridViewColumn colRemedyLastAction = new DataGridViewColumn(cellRemedyLastAction);
            colRemedyLastAction.HeaderText = "Remedy Last Action";
            colRemedyLastAction.DataPropertyName = "RemedyLastAction";
            

            DataGridViewColumn colResolvedDate = new DataGridViewColumn(cellRemedyResolvedDate);
            colResolvedDate.HeaderText = "Remedy Resolved Date";
            colResolvedDate.DataPropertyName = "RemedyResolvedDate";
            

            DataGridViewColumn colRemedyNotes = new DataGridViewColumn(cellRemedyNotes);
            colRemedyNotes.HeaderText = "Remedy Notes";
            colRemedyNotes.DataPropertyName = "RemedyNotes";

            datagridBase.Columns.Add(colRemedyId);
            datagridBase.Columns.Add(colInspectionProject);
            datagridBase.Columns.Add(colInspectionId);
            datagridBase.Columns.Add(colAssetId);
            datagridBase.Columns.Add(colInspectionResult);
            datagridBase.Columns.Add(colInspectionElectrical);
            datagridBase.Columns.Add(colInspectionMap);
            datagridBase.Columns.Add(colInspectionSection);
            datagridBase.Columns.Add(colDiscoveredDate);
            datagridBase.Columns.Add(colToptype);
            datagridBase.Columns.Add(colInspectionType);
            datagridBase.Columns.Add(colPreviusInspected);
            datagridBase.Columns.Add(colInspectedDetails);
            datagridBase.Columns.Add(colRemedyStatus);
            datagridBase.Columns.Add(colRemedyAssigned);
            datagridBase.Columns.Add(colRemedyLastAction);
            datagridBase.Columns.Add(colResolvedDate);
            datagridBase.Columns.Add(colRemedyNotes);

            datagridBase.AutoGenerateColumns = false;
            datagridBase.ReadOnly = false;

        }

        public void LoadData()
        {
            List<EInspection> listInspections = LInspection.SelectAllInspectionRemedies();
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn(m_ColumnId, typeof(string)));
            dt.Columns.Add(new DataColumn("InspectionProject", typeof(string)));
            dt.Columns.Add(new DataColumn("InspectionId", typeof(Int32)));
            dt.Columns.Add(new DataColumn("AssetId", typeof(Int32)));
            dt.Columns.Add(new DataColumn("InspectionResult", typeof(string)));
            dt.Columns.Add(new DataColumn("isElectrical", typeof(string)));
            dt.Columns.Add(new DataColumn("InspectionMap", typeof(string)));
            dt.Columns.Add(new DataColumn("InspectionSection", typeof(string)));
            dt.Columns.Add(new DataColumn("DiscoveredDate", typeof(string)));
            dt.Columns.Add(new DataColumn("PoleToptype", typeof(string)));
            dt.Columns.Add(new DataColumn("InspectionType", typeof(string)));
            dt.Columns.Add(new DataColumn("PreviusInspect", typeof(string)));
            dt.Columns.Add(new DataColumn("InspectDetails", typeof(string)));
            dt.Columns.Add(new DataColumn("RemedyStatus", typeof(Int32)));
            dt.Columns.Add(new DataColumn("RemedyAssigned", typeof(string)));
            dt.Columns.Add(new DataColumn("RemedyLastAction", typeof(string)));
            dt.Columns.Add(new DataColumn("RemedyResolvedDate", typeof(string)));
            dt.Columns.Add(new DataColumn("RemedyNotes", typeof(string)));

            dt.Columns.Add(new DataColumn("Inspection", typeof(object)));

            foreach (EInspection inspection in listInspections)
            {
                string inspectionResult = inspection.Result.Resultype.Name;
                if(inspection.Result.Resultype.Id == 2 && inspection.Result.IsElectrical)
                    inspectionResult += " Electrical";
                ERemedy tmpRemedy = inspection.CrossRemedies[inspection.CrossRemedies.Count - 1];
                Int32 remedyStatus = Int32.MinValue;
                object remedyResolvedDate = null;
                object remedyLastaction = null;
                object remedyAssigned = null;
                object isElectrical = null;
                object inspectionSection = null;
                object assetId = null;
                if (tmpRemedy.Status.Id != Int32.MinValue)
                    remedyStatus = tmpRemedy.Status.Id;
                if (tmpRemedy.ResolvedDate != DateTime.MinValue)
                    remedyResolvedDate = tmpRemedy.ResolvedDate;
                if (tmpRemedy.LastAction != DateTime.MinValue)
                    remedyLastaction = tmpRemedy.LastAction;
                if (!String.IsNullOrEmpty(tmpRemedy.Assigned))
                    remedyAssigned = tmpRemedy.Assigned;

                if (inspection.UtilityAsset.Id != Int32.MinValue)
                    assetId = inspection.UtilityAsset.Id;

                if (inspection.Section.Id != Int32.MinValue)
                    inspectionSection = inspection.Section.Id;

                if (inspection.Result.IsElectrical)
                    isElectrical = "YES";


                dt.Rows.Add(tmpRemedy.Id, inspection.Project.Name,inspection.Id,assetId, inspectionResult, isElectrical, 
                            inspection.Map.Name, inspectionSection, inspection.AcquisitionDate,
                            inspection.TopType.Name, inspection.Result.InspectionType.Name, "2000", "Click",
                            remedyStatus, remedyAssigned, remedyLastaction,
                            remedyResolvedDate, tmpRemedy.Notes, tmpRemedy);

            }
            datagridBase.DataSource = dt;
        }

        protected override void AfterOpenMenu()
        {
            cmiAction.Enabled = false;
            ShowContextMenu = false;
        }

        protected override void ValidateCell(DataGridViewCell cellToValidate)
        {
            if ((cellToValidate.ColumnIndex == 15 || cellToValidate.ColumnIndex == 16) && cellToValidate.Value != cellToValidate.OwningColumn.DefaultCellStyle.NullValue)
            {
                //parsing datetime
                DateTime tmpDatetime = DateTime.MinValue;
                if(DateTime.TryParse(cellToValidate.Value.ToString(), out tmpDatetime))
                {
                    datagridBase[cellToValidate.ColumnIndex, cellToValidate.RowIndex].ErrorText = string.Empty;
                    cellToValidate.Value = tmpDatetime.ToShortDateString();
                }else
                {
                    datagridBase[cellToValidate.ColumnIndex, cellToValidate.RowIndex].ErrorText = "Please insert a valid datetime ex 10/02/2008";
                    cellToValidate.Value = cellToValidate.OwningColumn.DefaultCellStyle.NullValue;
                }
            }
        }

        protected override bool ActivateCellChangedEvent(DataGridViewCell cellToValidate)
        {
            if((cellToValidate.ColumnIndex >= 13 && cellToValidate.ColumnIndex <= 17) && cellToValidate.Value != cellToValidate.OwningColumn.DefaultCellStyle.NullValue)
                return true;
            else
                return false;
        }

        protected override void datagridBase_DoubleClick(object sender, EventArgs e)
        {
            if (m_ActivateDoubleClick)
                OnClickedEdit(this, new GridBaseEventArgs(getSelectedId(), datagridBase.SelectedRows[0],((ERemedy) (((System.Data.DataRowView) (datagridBase.SelectedRows[0].DataBoundItem)).Row.ItemArray[18]))));

        }
    }
}
